<?php

require_once('functions/pageLoad.php');
loginRequired(true);
$user_session = explode("_", $_SESSION['user']);
$user_id = $user_session[0];

if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['jid']) && isset($_POST['message']) && isset($_POST['tid'])){

// find quote and verify user

$sql = "SELECT quotes.id, trade, area FROM quotes 
	LEFT JOIN jobs ON quotes.job_id = jobs.id
	LEFT JOIN trades ON jobs.trade_id = trades.id 
	LEFT JOIN areas ON jobs.area_id = areas.id 
	WHERE quotes.tradesman_id = '".mysql_real_escape_string($_POST['tid'])."' AND quotes.job_id = '".mysql_real_escape_string($_POST['jid'])."' AND jobs.user_id = '".$user_id."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$job_rs=mysql_fetch_assoc($query);

if(mysql_num_rows($query) > 0){

$sql = "INSERT INTO messages (job_id, tradesman_id, user_id, message_date, message) VALUES ('".mysql_real_escape_string($_POST['jid'])."','".mysql_real_escape_string($_POST['tid'])."','".$user_id."',NOW(),'".mysql_real_escape_string(stripslashes($_POST['message']))."')";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

$job_link = build_job_link($job_rs['trade'],$job_rs['area'],$_POST['jid']);

// send email to tradesman

$sql = "SELECT email, firstname, surname FROM users 
	LEFT JOIN tradesman ON users.id = tradesman.user_id 
	WHERE tradesman.id = '".mysql_real_escape_string($_POST['tid'])."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$user_rows = mysql_num_rows($query);
$user_rs=mysql_fetch_assoc($query);

require_once("classes/class.phpmailer.php");
	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "localhost"; // SMTP server
	$mail->From = "mail@iwantatradesman.co.uk";
	$mail->FromName = "I Want A Tradesman";

	$mail->AddAddress($user_rs['email']);

	$mail->Subject = "Private message notification";
	$mail->Body = "Dear ".$user_rs['firstname']." ".$user_rs['surname']."\r\n\r\nA user has sent you a private message regarding a quote you have submitted for their job on I Want A Tradesman.\r\n\r\nYou can view the message here: http://www.iwantatradesman.co.uk".$job_link."\r\n\r\nKind regards\r\n\r\nThe I Want A Tradesman Team\r\n\r\n\r\nThis email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.";

	$mail->Send();

header("Location: ".$job_link."?result=message");

}

}

?>